package com.iqiyi.pps.epg.core.service.sginal;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.sginal.TSginalLog;
import com.iqiyi.pps.epg.api.model.web.sginal.TSginalLogDataPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.sginal.SginalLogDataDao;
import com.iqiyi.pps.epg.core.model.sginal.SginalLog;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 2017/8/14.
 */
@Service
@Transactional
public class SginalLogServiceImpl implements SginalLogService {
    private Logger logger = LoggerFactory.getLogger(SginalLogServiceImpl.class);

    public static final String SUCCESS = "操作成功";
    public static final String FAIL = "操作失败";

    public static final String ACTION_DEL = "删除频道";
    public static final String ACTION_FORCEDEL = "强制删除频道";
    public static final String ACTION_ADD = "添加频道";
    public static final String ACTION_EDIT = "修改频道";
    public static final String ACTION_START = "启动频道";
    public static final String ACTION_STOP = "停止频道";

    public static final String ACTION_ADD_SIGNAL = "添加频道信号";
    public static final String ACTION_EDIT_SIGNAL = "修改频道信号";

    public static final String SYSTEM_DEL = "删除频道(API)";
    public static final String SYSTEM_ADD = "添加频道(API)";
    public static final String SYSTEM_EDIT = "修改频道(API)";
    public static final String SYSTEM_START = "启动频道(API)";
    public static final String SYSTEM_STOP = "停止频道(API)";
    public static final String SYSTEM_STATUS = "验证服务器(API)";

    public static final String SYSTEM_NAME = "system";

    @Autowired
    private SginalLogDataDao dao;

    @Override
    public void saveLog(int fatherId, String action, String url, String response, String params, String user) {
        SginalLog sginalLog = new SginalLog();
        sginalLog.setFatherId(fatherId);
        sginalLog.setAction(action);
        StringBuffer descr = new StringBuffer();

        if (StringUtils.isNotEmpty(params)) {
            descr.append(params);
        }
        if (StringUtils.isNotEmpty(url)) {
            descr.append(" | URL: ").append(url);
        }
        if (StringUtils.isNotEmpty(response)) {
            descr.append(" | RESPONSE: ").append(response);
        } else if (action.endsWith("(API)")) {
            descr.append(" | RESPONSE: null");
        }
        sginalLog.setDescr(descr.toString());
        sginalLog.setUser(user);
        dao.saveOnly(sginalLog);
    }

    @Override
    public Long add(SginalLog sginalLog) {
        dao.saveOnly(sginalLog);
        return sginalLog.getId();
    }

    @Override
    public TSginalLogDataPage getSginalLogData(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        String tempPattern = "${PARAMS}";
        String totalPattern = "select COUNT(*)";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String hql = tempPattern + " from com.iqiyi.pps.epg.core.model.sginal.SginalLog";
        Page<SginalLog> dbPage = TransUtil.buildPage(page);

        hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, hql, filterList);

        if (dbPage.isAutoCount()) {
            String totalCount = dao.getSession().createQuery(hql.replace(tempPattern, totalPattern)).uniqueResult().toString();
            page.setTotalCount(Long.valueOf(totalCount));
        }

        List result = dao.getSession().createQuery(hql.replace(tempPattern, ""))
                .setMaxResults(dbPage.getPageSize())
                .setFirstResult((dbPage.getPageNo() - 1) * dbPage.getPageSize())
                .list();
        TSginalLogDataPage tSginalLogDataPage = new TSginalLogDataPage();
        tSginalLogDataPage.setTPage(page);
        List<TSginalLog> tServerDatas = new ArrayList<TSginalLog>();
        for(Object obj : result) {
            SginalLog sginalLog = (SginalLog) obj;
            tServerDatas.add(sginalLog.convertThriftModel());
        }
        tSginalLogDataPage.setResult(tServerDatas);
        return tSginalLogDataPage;
    }
}
